//
//  FKCommodityCommentCell.m
//  HappyTest
//
//  Created by Macbook Pro on 2019/2/16.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKCommodityCommentCell.h"
#import "FKCommodityComment.h"

@interface FKCommodityCommentCell ()

@property (nonatomic, weak) UIImageView *avatarView;
@property (nonatomic, weak) UILabel *nameLabel;
@property (nonatomic, weak) UILabel *timeLabel;
@property (nonatomic, weak) UIImageView *starView;
@property (nonatomic, weak) UILabel *contentLabel;
@property (nonatomic, weak) UIView *container;
@property (nonatomic, strong) UIView *emptyView;

@end

@implementation FKCommodityCommentCell

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews
{
    self.backgroundColor = [UIColor whiteColor];
    
    UIImageView *avatarView = [[UIImageView alloc] init];
    [self.contentView addSubview:avatarView];
    self.avatarView = avatarView;
    [avatarView fk_viewCornerRadius:fk_adjustW(15) borderWidth:0 borderColor:nil];
    
    UILabel *nameLabel = [UILabel fk_labelWithFont:fk_adjustFont(16) textColor:fkColor333333];
    [self.contentView addSubview:nameLabel];
    self.nameLabel = nameLabel;
    
    UILabel *timeLabel = [UILabel fk_labelWithFont:fk_adjustFont(12) textColor:fkColor999999];
    [self.contentView addSubview:timeLabel];
    self.timeLabel = timeLabel;
    
    UIImageView *starView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"star_5"]];
    [self.contentView addSubview:starView];
    self.starView = starView;
    
    UILabel *contentLabel = [UILabel fk_labelWithFont:fk_adjustFont(14) textColor:fkColor333333];
    [self.contentView addSubview:contentLabel];
    self.contentLabel = contentLabel;
    
    UIView *container = [[UIView alloc] init];
    [self.contentView addSubview:container];
    self.container = container;
    
    for (NSInteger index = 0; index < 9; index++) {
        UIImageView *pictureView = [[UIImageView alloc] init];
        [container addSubview:pictureView];
        pictureView.userInteractionEnabled = true;
        [pictureView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pictureViewDidTap:)]];
    }
    
    // 设置frame
    [avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(fk_adjustW(30));
        make.left.offset(15);
        make.top.offset(14);
    }];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(avatarView.mas_right).offset(8);
        make.centerY.equalTo(avatarView);
    }];
    [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-12);
        make.centerY.equalTo(avatarView);
    }];
    [starView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(avatarView);
        make.top.equalTo(avatarView.mas_bottom).offset(12);
    }];
    [contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(avatarView);
        make.right.equalTo(timeLabel);
        make.top.equalTo(starView.mas_bottom).offset(fk_adjustW(16));
    }];
    [container mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.offset(0);
        make.top.equalTo(contentLabel.mas_bottom).offset(14);
    }];
    
}

- (void)pictureViewDidTap:(UITapGestureRecognizer *)gesture
{
    NSInteger index = [self.container.subviews indexOfObject:gesture.view];
    if ([self.delegate respondsToSelector:@selector(commodityCommentCell:didTapPicture:)]) {
        [self.delegate commodityCommentCell:self didTapPicture:index];
    }
}


- (UIView *)emptyView
{
    if (!_emptyView) {
        _emptyView = [[UIView alloc] init];
        [self.contentView addSubview:_emptyView];
        _emptyView.hidden = true;
        _emptyView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [_emptyView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.offset(0);
        }];
        
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_data"]];
        [_emptyView addSubview:imageView];
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.offset(0);
            make.width.mas_equalTo(fk_adjustW(imageView.image.size.width));
            make.height.mas_equalTo(fk_adjustW(imageView.image.size.height));
        }];
        
        UILabel *label = [[UILabel alloc] init];
        [_emptyView addSubview:label];
        label.font = fk_adjustFont(16);
        label.text = @"暂无评价";
        label.textColor = fkColor999999;
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(imageView.mas_bottom).offset(20);
            make.centerX.offset(0);
        }];
    }
    return _emptyView;
}

- (void)setComment:(FKCommodityComment *)comment
{
    _comment = comment;
    
    if (!comment) {
        self.emptyView.hidden = false;
    } else {
        self.emptyView.hidden = true;
        
        [self.avatarView sd_setImageWithURL:[NSURL URLWithString:comment.user_head] placeholderImage:kDefaultAvatar];
        self.nameLabel.text = comment.user_nickname;
        self.timeLabel.text = comment.addTime;
        self.contentLabel.text = comment.content;
        for (NSInteger index = 0; index < self.container.subviews.count; index++) {
            UIImageView *picView = self.container.subviews[index];
            if (index < comment.imgFrames.count) {
                picView.frame = [comment.imgFrames[index] CGRectValue];
                [picView sd_setImageWithURL:[NSURL URLWithString:comment.img[index]] placeholderImage:kPlaceholder_1x1];
            } else {
                picView.frame = CGRectZero;
            }
        }
    }
}

@end
